package cn.iocoder.yudao.module.biz.controller.admin.htHiddentrouble;

import cn.iocoder.yudao.framework.security.core.LoginUser;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.biz.service.htHiddentroubleannexs.HtHiddentroubleannexsService;
import cn.iocoder.yudao.module.infra.dal.dataobject.oss.OssDO;
import cn.iocoder.yudao.module.infra.service.oss.OssService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.*;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.biz.controller.admin.htHiddentrouble.vo.*;
import cn.iocoder.yudao.module.biz.dal.dataobject.htHiddentrouble.HtHiddentroubleDO;
import cn.iocoder.yudao.module.biz.convert.htHiddentrouble.HtHiddentroubleConvert;
import cn.iocoder.yudao.module.biz.service.htHiddentrouble.HtHiddentroubleService;

@Api(tags = "管理后台 - 隐患信息")
@RestController
@RequestMapping("/biz/ht-hiddentrouble")
@Validated
public class HtHiddentroubleController {

    @Resource
    private HtHiddentroubleService htHiddentroubleService;
    @Resource
    private OssService ossService;
    @Resource
    private HtHiddentroubleannexsService hiddentroubleannexsService;

    @PostMapping("/create")
    @ApiOperation("创建隐患信息")
//    @PreAuthorize("@ss.hasPermission('biz:ht-hiddentrouble:create')")
    public CommonResult<String> createHtHiddentrouble(@Valid @RequestBody HtHiddentroubleCreateReqVO createReqVO) {
        return success(htHiddentroubleService.createHtHiddentrouble(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新隐患信息")
//    @PreAuthorize("@ss.hasPermission('biz:ht-hiddentrouble:update')")
    public CommonResult<Boolean> updateHtHiddentrouble(@Valid @RequestBody HtHiddentroubleUpdateReqVO updateReqVO) {
        htHiddentroubleService.updateHtHiddentrouble(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除隐患信息")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = String.class)
//    @PreAuthorize("@ss.hasPermission('biz:ht-hiddentrouble:delete')")
    public CommonResult<Boolean> deleteHtHiddentrouble(@RequestParam("id") String id) {
        htHiddentroubleService.deleteHtHiddentrouble(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得隐患信息")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = String.class)
//    @PreAuthorize("@ss.hasPermission('biz:ht-hiddentrouble:query')")
    public CommonResult<HiddentroubleJoinVO> getHtHiddentrouble(@RequestParam("id") String id) {
        HiddentroubleJoinVO HiddentroubleJoinVO = htHiddentroubleService.queryById(id);
        return success(HiddentroubleJoinVO);
    }

    @GetMapping("/list")
    @ApiOperation("获得隐患信息列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('biz:ht-hiddentrouble:query')")
    public CommonResult<List<HtHiddentroubleRespVO>> getHtHiddentroubleList(@RequestParam("ids") Collection<String> ids) {
        List<HtHiddentroubleDO> list = htHiddentroubleService.getHtHiddentroubleList(ids);
        return success(HtHiddentroubleConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得隐患信息分页")
//    @PreAuthorize("@ss.hasPermission('biz:ht-hiddentrouble:query')")
    public CommonResult<PageResult<HiddentroublePageJoinVO>> getHtHiddentroublePage(@Valid HtHiddentroublePageReqVO pageVO) {
        PageResult<HiddentroublePageJoinVO> result=new PageResult<>();
        int pageNo=pageVO.getPageNo();
        pageVO.setPageNo((pageNo-1)*pageVO.getPageSize());

        //登录用户ID
        Long userId = SecurityFrameworkUtils.getLoginUserId();
        pageVO.setCheckPeoId(userId);

        List<HiddentroublePageJoinVO> list = htHiddentroubleService.selectHiddentroublePage(pageVO);
        Integer count= htHiddentroubleService.selectHiddentroubleCount(pageVO);
        result.setList(list);
        result.setTotal((long) count);
        return success(result);
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出隐患信息 Excel")
    @PreAuthorize("@ss.hasPermission('biz:ht-hiddentrouble:export')")
    @OperateLog(type = EXPORT)
    public void exportHtHiddentroubleExcel(@Valid HtHiddentroubleExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<HtHiddentroubleDO> list = htHiddentroubleService.getHtHiddentroubleList(exportReqVO);
        // 导出 Excel
        List<HtHiddentroubleExcelVO> datas = HtHiddentroubleConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "隐患信息.xls", "数据", HtHiddentroubleExcelVO.class, datas);
    }

}
